import 'dart:io';
import 'dart:typed_data';
import 'dart:ui';

import 'package:fate_blind_box/network/Api.dart';
import 'package:fate_blind_box/network/http_config.dart';
import 'package:fate_blind_box/utils/loading.dart';
import 'package:fate_blind_box/utils/provider.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/services.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:fluwx/fluwx.dart';
import 'package:get/get.dart';
import 'package:image_gallery_saver/image_gallery_saver.dart';
import 'state.dart';

class MemberCodeLogic extends GetxController {
  final MemberCodeState state = MemberCodeState();

  @override
  void onInit() {
    // TODO: implement onInit
    super.onInit();
    if(Get.arguments!=null){
      state.dataModel = Get.arguments;
    }
    _getQr();
  }

  _getQr() {
    loading(() async {
      final res = await Api.getInstance().getMemberInfo(Provider.memberId);
      if (res?['data']?['shareCode']?.toString().isNotEmpty == true) {
        Provider.nickName = res['data']['nickName'];
        state.inviteCode.value = res['data']['shareCode'];
        state.profileNo.value = res['data']['profileNo'];
      } else {
        toast("获取邀请码失败！");
      }
    });
  }

  onClick(String id) {
    switch (id) {
      case 'wechat':
      //个人设置
        String asset = 'images/ic_inviteLogo.png';
        String asset1 = Provider.nickName;
        var model = WeChatShareWebPageModel(
          _getQrInfo(),
          title: '偶然遇见另一半',
          scene: WeChatScene.SESSION,
          description: '<$asset1>\n邀请您参加偶然遇见、寻找您心仪的另一半，开启您的幸福之旅！',
          thumbnail: WeChatImage.asset(asset, suffix: 'png'),
        );
        shareToWeChat(model);
        break;
      case 'circle':
        String asset = 'images/ic_inviteLogo.png';
        String linkUrl =
            "https://thirdwx.qlogo.cn/mmopen/vi_32/DYAIOgq83erg9HmcT5ld2GYIBaqCK2KB25dUSSqYFRgjjqkJWDZAn8f0In7nVQ0dylVWlPA8aBh8XYUL0RzTZQ/132";
        var model = WeChatShareWebPageModel(
          _getQrInfo(),
          title: '偶然遇见另一半',
          scene: WeChatScene.TIMELINE,
          description: '<${Provider.nickName}>\n邀请您参加偶然遇见、寻找您心仪的另一半，开启您的幸福之旅！',
          mediaTagName: 'dd',
          thumbnail: WeChatImage.asset(asset, suffix: 'png'),
        );

        shareToWeChat(model);
        break;
      case 'QQ':
        break;
      case 'qzone':
        break;
    }
  }

  String _getQrInfo() {
    return state.inviteCode.isEmpty
        ? ""
        : HttpConfig.baseUrl + "?invite_code=${state.inviteCode}";
  }

  saveLocImage()async{
    RenderRepaintBoundary boundary = state.printKey
        .currentContext!
        .findRenderObject() as RenderRepaintBoundary;
    var image = await boundary.toImage(pixelRatio: 3.0);
    ByteData? byteData = await image.toByteData(format: ImageByteFormat.png);
    Uint8List? pngBytes = byteData?.buffer.asUint8List();
    if (pngBytes != null) {
      final result = await ImageGallerySaver.saveImage(pngBytes);
      if (result['isSuccess']) {
        EasyLoading.showToast('成功保存到相册中');
      } else {
        EasyLoading.showToast('保存失败');
      }
    }
  }
}
